package com.its.oa.system.permission.service.impl;

import java.util.List;

import com.its.oa.model.Module;
import com.its.oa.model.Permission;
import com.its.oa.model.Role;
import com.its.oa.system.module.dao.ModuleDao;
import com.its.oa.system.permission.dao.PermissionDao;
import com.its.oa.system.permission.service.PermissionService;
import com.its.oa.util.EntityValidata;

public class PermissionServiceImpl implements PermissionService {

	private PermissionDao permissionDao;
	private ModuleDao moduleDao;

	public void setPermissionDao(PermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}
	
	public void setModuleDao(ModuleDao moduleDao) {
		this.moduleDao = moduleDao;
	}

	public String findUrlListByRoleList(List<Role> roleList) {
		String urls = "";
		List<Permission> pmsList = permissionDao.findPmsListByRoleList(roleList);
		for (Permission permission : pmsList) {
			urls += EntityValidata.isNotEmpty(urls)?","+permission.getUrl():permission.getUrl();
		}
		return urls;
	}
	public String findPmsidByUrl(String url) {
		Permission p = permissionDao.findPermissionByUrl(url);
		if(p!=null)
			return p.getId().toString();	
		else
			return null;
	}
	
	public List<Permission> findAll(){
		return permissionDao.findAll();
	}
	
	public List<Permission> findAllPermissionByPage(int offset,int pagesize) {
		return permissionDao.findAllByPage(offset,pagesize);
	}
	public void addPms(int moduleid, String pmscode, String pmsname, String url) {
		Module module = moduleDao.findById(moduleid);
		Permission pms = new Permission();
		pms.setModule(module);
		pms.setCode(pmscode);
		pms.setName(pmsname);
		pms.setUrl(url);
		permissionDao.save(pms);
	}

}
